/*<![CDATA[*/

/*
    Copyright (c) 2009, Samad Ghasemi
    All rights reserved.

    Redistribution and use in source and binary forms, with or without 
    modification, are permitted provided that the following conditions are met:

        * Redistributions of source code must retain the above copyright 
          notice, this list of conditions and the following disclaimer.
        * Redistributions in binary form must reproduce the above copyright 
          notice, this list of conditions and the following disclaimer in the 
          documentation and/or other materials provided with the distribution.
        * Neither the name of the <ORGANIZATION> nor the names of its 
          contributors may be used to endorse or promote products derived from 
          this software without specific prior written permission.

    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
    AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
    IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
    ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 
    LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
    CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
    SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
    INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
    CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
    ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
    POSSIBILITY OF SUCH DAMAGE.
*/

/*
TODO

NOTE
*/

var first = true;

function start()
{
    if(first)
    {
        document.getElementById('input').value = "";
        first = false;
    }
    document.getElementById('input').focus();
    //document.getElementById("converted").readOnly = true;
}

function convert()
{
	document.getElementById('converted').value = replace_string(document.getElementById('input').value);
    //changes the postition of the converted textarea so the latest converted part is alwasy visible
    document.getElementById('converted').scrollTop = document.getElementById('converted').scrollHeight;
}

//Takes a string and converts it from Fingilish to Farsi
function replace_string(str)
{
	//if the separator ' used it's not a combo any more!
    str = str.replace(/'H/g, "\u062D");
    str = str.replace(/'h/g, "\u0647");

	/*
	* letters whic differ when used at the beginning or end of the words/sentences
	* or the ones only used for connecting the words (in speaking)
	*/
    str = str.replace(/ e /ig, " ");    //e.g. mard e bozorg
    str = str.replace(/ a /ig, " ");    //e.g. 
    str = str.replace(/ o /ig, " \u0648 "); //e.g. man o to
    
    str = str.replace(/ e\b/ig, "");
    str = str.replace(/ o\b/ig, " \u0648");

    str = str.replace(/\boo/ig, "\u0627\u0648");
    str = str.replace(/\bi/ig, "\u0627\u06CC");
    str = str.replace(/\bo/ig, "\u0627");
    str = str.replace(/\be/ig, "\u0627");
    str = str.replace(/\bu/ig, "\u0627\u0648");
    
    str = str.replace(/e\b/ig, "\u0647");   //e.g. esmet chie
    str = str.replace(/o\b/ig, "\u0648");   //e.g. (2) do

	//combos
    str = str.replace(/ch/ig, "\u0686");
    str = str.replace(/kh/ig, "\u062E");
    str = str.replace(/oo/ig, "\u0648");
    str = str.replace(/ou/ig, "\u0648");
    str = str.replace(/th/ig, "\u062B");
    str = str.replace(/sh/ig, "\u0634");

	//main single characters (the ones with only one letter in Farsi and no other possibility):
    str = str.replace(/A/g, "\u0622");
    str = str.replace(/a/g, "\u0627");
    //yeah we know vowels by heart!
    str = str.replace(/e/ig, "");
    str = str.replace(/o/ig, "");

	str = str.replace(/b/ig, "\u0628");
	str = str.replace(/p/ig, "\u067E");
	str = str.replace(/d/ig, "\u062F");
	str = str.replace(/r/ig, "\u0631");
	str = str.replace(/f/ig, "\u0641");
    str = str.replace(/c/ig, "\u06A9"); //ci & ce in the combos!
	str = str.replace(/k/ig, "\u06A9");
	str = str.replace(/g/ig, "\u06AF");
	str = str.replace(/l/ig, "\u0644");
	str = str.replace(/m/ig, "\u0645");
	str = str.replace(/n/ig, "\u0646");
	str = str.replace(/v/ig, "\u0648");
	str = str.replace(/u/ig, "\u0648");
	str = str.replace(/i/ig, "\u06CC");
    str = str.replace(/w/ig, "\u0639");
    str = str.replace(/q/g, "\u0642");
    str = str.replace(/Q/g, "\u063A");
    str = str.replace(/X/g, "\u0638");
    str = str.replace(/x/g, "\u0636");
    str = str.replace(/H/g, "\u062D");
    str = str.replace(/h/g, "\u0647");
    //str = str.replace(/Y/g, "");    //balay e kooh
	str = str.replace(/y/g, "\u06CC");
    str = str.replace(/Y/g, "\u0621");
    str = str.replace(/T/g, "\u0637");
	str = str.replace(/t/g, "\u062A");
    str = str.replace(/J/g, "\u0698");
	str = str.replace(/j/g, "\u062C");
    str = str.replace(/Z/g, "\u0630");
	str = str.replace(/z/g, "\u0632");
    str = str.replace(/S/g, "\u0635");
	str = str.replace(/s/g, "\u0633");
	//numbers:
	str = str.replace(/0/g, "\u06F0");
	str = str.replace(/1/g, "\u06F1");
	str = str.replace(/2/g, "\u06F2");
	str = str.replace(/3/g, "\u06F3");
	str = str.replace(/4/g, "\u06F4");
	str = str.replace(/5/g, "\u06F5");
	str = str.replace(/6/g, "\u06F6");
	str = str.replace(/7/g, "\u06F7");
	str = str.replace(/8/g, "\u06F8");
	str = str.replace(/9/g, "\u06F9");
	//special characters: (maybe more later)
	str = str.replace(/\?/g, "\u061F");
	str = str.replace(/,/g, "\u060C");
	str = str.replace(/;/g, "\u061B");
    str = str.replace(/%/g, "\u066a");

    return str;
}

function highlight()
{
	document.getElementById("converted").focus();
	document.getElementById("converted").select();
	//copy_to_clipboard(document.getElementById("converted").value);
}

/*
//only works in IE!
function copy_to_clipboard(con_str)
{
	if( window.clipboardData && clipboardData.setData )
	{
		clipboardData.setData("Text", con_str);
		alert("The contents have been copied to your clipboard.\t");
	}
	else
	{
		//nothing, only IE supports this?!
	}
}
*/


/*]]>*/